using System;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using ClimbTracker.GUI.Utils;

namespace ClimbTracker.GUI.TabControls
{
    public partial class SketchTabControl : UserControl
    {
        private const int EPSILON = 5;

        // Keep track of when fake drag or resize mode is enabled.
        private bool isDragging = false;
        private bool isResizing = false;

        // Store the location where the user clicked on the control.
        private int clickOffsetX, clickOffsetY;
        private int startX, startY;

        Color selectedColor = Color.Blue;

        private static IList<HotSpot> _hotspotControlsList = new List<HotSpot>();
        private static int[] _hotspotRectCoordinates;

        public static int[] HotspotRectCoordinates
        {
            get { return SketchTabControl._hotspotRectCoordinates; }
            set { SketchTabControl._hotspotRectCoordinates = value; }
        }
        public static IList<HotSpot> HotspotControlsList
        {
            get { return SketchTabControl._hotspotControlsList; }
            set { SketchTabControl._hotspotControlsList = value; }
        }


        public SketchTabControl()
        {
            InitializeComponent();
        }



        private void label_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active label.
            Control currentControl;
            currentControl = (Control)sender;

            if (e.Button == MouseButtons.Right)
            {
                // Show the context menu.
                currentControl.ContextMenu.Show(currentControl, new Point(e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            {
                clickOffsetX = e.X;
                clickOffsetY = e.Y;

                if (e.X < EPSILON  ||  e.Y < EPSILON  ||  currentControl.Width - e.X < EPSILON  ||  currentControl.Height - e.Y < EPSILON)
                    //(e.X + 5) > currentCtrl.Width || (e.Y + 5) > currentCtrl.Height)
                {
                    // The mouse pointer is in the bottom right corner,
                    // so resizing mode is appropriate.
                    isResizing = true;
                }
                else
                {
                    // The mouse is somewhere else, so dragging mode is
                    // appropriate.
                    isDragging = true;
                }
            }
        }

        private void label_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active shape.
            Control currentControl;
            currentControl = (Control)sender;

            if (isDragging)
            {
                // Move the control.
                currentControl.Left = e.X + currentControl.Left - clickOffsetX;
                currentControl.Top = e.Y + currentControl.Top - clickOffsetY;
            }
            else if (isResizing)
            {
                // Resize the control, according to the resize mode.
                if (currentControl.Cursor == Cursors.SizeNWSE)
                {
                    currentControl.Width = e.X;
                    currentControl.Height = e.Y;
                }
                else if (currentControl.Cursor == Cursors.SizeNS)
                {
                    currentControl.Height = e.Y;
                }
                else if (currentControl.Cursor == Cursors.SizeWE)
                {
                    currentControl.Width = e.X;
                }
            }
            else
            {
                // Change the cursor if the mouse pointer is on one of the edges
                // of the control.

                if (e.X < EPSILON)
                {
                    if (e.Y < EPSILON)
                    {
                        currentControl.Cursor = Cursors.SizeNWSE;
                    }
                    else if ((currentControl.Height - e.Y) < EPSILON)
                    { 
                        currentControl.Cursor = Cursors.SizeNESW;
                    }
                    else
                    {
                        currentControl.Cursor = Cursors.SizeWE;
                    }
                }
                else if (currentControl.Width - e.X < EPSILON)
                {
                    if (e.Y < EPSILON)
                    {
                        currentControl.Cursor = Cursors.SizeNESW;
                    }
                    else if ((currentControl.Height - e.Y) < EPSILON)
                    {
                        currentControl.Cursor = Cursors.SizeNWSE;
                    }
                    else
                    {
                        currentControl.Cursor = Cursors.SizeWE;
                    }
                }
                else if (e.Y < EPSILON || currentControl.Height - e.Y < EPSILON)
                {
                    currentControl.Cursor = Cursors.SizeNS;
                }
                else
                {
                    currentControl.Cursor = Cursors.SizeAll;
                }
            }
        }

        private void label_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDragging = false;
            isResizing = false;
        }

        private void menuColorChange_Click(object sender, System.EventArgs e)
        {
            // Show color dialog.
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.ShowDialog();

            // Change label border color.
            menuLabel.SourceControl.ForeColor = colorDialog.Color;
        }


        private void menuRemoveShape_Click(object sender, System.EventArgs e)
        {
            HotSpot sourceControl = (HotSpot)menuLabel.SourceControl;
            sketchPanel.Controls.Remove(sourceControl);
            int i = _hotspotControlsList.IndexOf(sourceControl);
            _hotspotControlsList.Remove(sourceControl);

            // Update numbers of labels
            for(; i < _hotspotControlsList.Count; i++)
            {
                int j = i + 1;
                _hotspotControlsList[i].InnerText = j.ToString();
            }

            // Refresh the panel so that all children controls will be redrawn with the updated numbers.
            sketchPanel.Refresh();

        }


        private void SketchTabControl_Load(object sender, EventArgs e)
        {
            sketchPanel.BackgroundImage = new Bitmap("c:/lena.jpg");
            sketchPanel.Size = sketchPanel.BackgroundImage.Size;


            // Adjust the sketch panel size if the picture is small enough
            bool isWidthReduced = false;
            bool isHeightReduced = false;
            if (sketchPanel.BackgroundImage.Width < containingPanel.Width)
            {
                containingPanel.Width = sketchPanel.BackgroundImage.Width;
                isWidthReduced = true;
            }
            if (sketchPanel.BackgroundImage.Height < containingPanel.Height)
            {
                containingPanel.Height = sketchPanel.BackgroundImage.Height;
                isHeightReduced = true;
            }
            if(isWidthReduced  &&  isHeightReduced)
                containingPanel.AutoScroll = false;
        }




        private void sketchPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // Create and configure the shape with some defaults.
            HotSpot newHotSpot = new HotSpot();
            newHotSpot.Size = new Size(40, 40);
            newHotSpot.ForeColor = currentColorLabel.BackColor;


            // Add the control to the hotspots collections
            _hotspotControlsList.Add(newHotSpot);
            newHotSpot.InnerText = _hotspotControlsList.Count.ToString();

            // To determine where to place the shape, you need to convert the 
            // current screen-based mouse coordinates into relative form coordinates.
            newHotSpot.Location = e.Location;// this.PointToClient(Control.MousePosition);

            // Attach a context menu to the shape.
            newHotSpot.ContextMenu = menuLabel;

            // Connect the shape to all its event handlers.
            newHotSpot.MouseDown += new MouseEventHandler(label_MouseDown);
            newHotSpot.MouseMove += new MouseEventHandler(label_MouseMove);
            newHotSpot.MouseUp += new MouseEventHandler(label_MouseUp);

            // Add the shape to the form.
            sketchPanel.Controls.Add(newHotSpot);

        }

        private void chooseColorLabel_Click(object sender, EventArgs e)
        {
            // Show color dialog.
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.ShowDialog();

            // Change label border color.
            currentColorLabel.BackColor = colorDialog.Color;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            // TODO: update DB with wall installations

            // Create a int array (size = 4 * number of hotspots) that will hold
            // the x, y, width, height of each hotspot
            _hotspotRectCoordinates = new int[_hotspotControlsList.Count * 4];

            // Fill the int array
            for (int i = 0; i < _hotspotControlsList.Count; i++)
            {
                _hotspotRectCoordinates[i * 4 + 0] = _hotspotControlsList[i].Location.X;
                _hotspotRectCoordinates[i * 4 + 1] = _hotspotControlsList[i].Location.Y;
                _hotspotRectCoordinates[i * 4 + 2] = _hotspotControlsList[i].Width;
                _hotspotRectCoordinates[i * 4 + 3] = _hotspotControlsList[i].Height;
            }

        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            foreach(HotSpot hotspot in _hotspotControlsList)
            {
                sketchPanel.Controls.Remove(hotspot);
            }

            _hotspotControlsList.Clear();
            sketchPanel.Refresh();
        }

    }
}


